//
//  LYSettingAccountCancelAlertView.swift
//  LYOnlineBanking
//
//  Created by 李永彬 on 2025/7/27.
//

import UIKit

class LYSettingAccountCancelAlertView: UIView {

    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.boldSystemFont(ofSize: 16)
        label.textColor = UIColor(hex: "#000000")
        label.textAlignment = .center
        label.text = LYlocalizedString("Account Cancellation")
        return label
    }()
    
    private lazy var tipsLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.boldSystemFont(ofSize: 14)
        label.numberOfLines = 0
        label.textColor = UIColor(hex: "#000000")
        label.textAlignment = .left
        label.text = LYlocalizedString("Please read the following content carefully:")
        return label
    }()

    private lazy var vStackView: UIStackView = {
        let stack = UIStackView()
        stack.axis = .vertical
        stack.alignment = .fill
        stack.distribution = .equalSpacing
        stack.spacing = 10 // 设置子视图间距
        stack.translatesAutoresizingMaskIntoConstraints = false
        return stack
    }()
    
    private lazy var hStackView: UIStackView = {
        let stack = UIStackView()
        stack.axis = .horizontal
        stack.alignment = .fill
        stack.distribution = .fillEqually
        stack.spacing = 15 // 设置子视图间距
        stack.translatesAutoresizingMaskIntoConstraints = false
        stack.addArrangedSubview(deleteButton)
        stack.addArrangedSubview(cancelButton)
        return stack
    }()
    
    lazy var deleteButton: UIButton = {
        let button = UIButton(type: .custom)
        button.addTarget(self, action: #selector(deleteButtonAction(_:)), for: .touchUpInside)
        button.setTitleColor(UIColor(hex: "#7F85A6"), for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .semibold)
        button.setTitle(LYlocalizedString("Delete Account"), for: .normal)
        button.backgroundColor = UIColor(hex: "#DCE4FF")
        button.layer.masksToBounds = true
        button.layer.cornerRadius = (45 * xScale) / 2.0
        return button
    }()
    
    lazy var cancelButton: UIButton = {
        let button = UIButton(type: .custom)
        button.addTarget(self, action: #selector(cancelButtonAction(_:)), for: .touchUpInside)
        button.setTitleColor(UIColor(hex: "#FFFFFF"), for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .semibold)
        button.setTitle(LYlocalizedString("Cancel"), for: .normal)
        button.backgroundColor = UIColor(hex: "#1E4FF3")
        button.layer.masksToBounds = true
        button.layer.cornerRadius = (45 * xScale) / 2.0
        return button
    }()
    
    let policyView = LYAccountAlertPolicyView()
    
    var deleteActionClosure: ((Bool) -> Void)?
    var cancelActionClosure: (() -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInitSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func commonInitSubviews() {
        addSubview(titleLabel)
        addSubview(tipsLabel)
        addSubview(vStackView)
        addSubview(hStackView)
        addSubview(policyView)
        
        setLayoutSubviews()
        
        let contents = [LYlocalizedString("alert_tips_1"),
                        LYlocalizedString("alert_tips_2"),
                        LYlocalizedString("alert_tips_3"),
                        LYlocalizedString("alert_tips_4")]
        setDatas(contents: contents)
    }
    
    func setLayoutSubviews() {
        titleLabel.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview().inset(16)
            make.top.equalToSuperview().offset(20)
            make.height.equalTo(20)
        }
        
        tipsLabel.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview().inset(16)
            make.top.equalTo(titleLabel.snp.bottom).offset(10)
            make.height.equalTo(20)
        }
        
        vStackView.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview().inset(16)
            make.top.equalTo(tipsLabel.snp.bottom).offset(10)
        }
        
        policyView.snp.makeConstraints { make in
            make.bottom.equalToSuperview().offset(-20)
            make.leading.trailing.equalToSuperview().inset(16)
            make.height.equalTo(20)
        }
        
        hStackView.snp.makeConstraints { make in
            make.bottom.equalTo(policyView.snp.top).offset(-25)
            make.leading.trailing.equalToSuperview().inset(25)
            make.height.equalTo(45 * xScale)
        }
    }
    
    func setDatas(contents: [String]) {
        vStackView.arrangedSubviews.forEach { $0.removeFromSuperview() } // 清空旧视图
        for content in contents {
            let label = UILabel()
            label.text = content
            label.font = UIFont.systemFont(ofSize: 14)
            label.textColor = UIColor(hex: "#000000")
            label.numberOfLines = 0
            label.textAlignment = .left
            
            let iv = UIImageView()
            iv.contentMode = .scaleAspectFit
            iv.backgroundColor = .clear
            iv.image = UIImage(named: "account_cancel_star")
            iv.bounds = CGRect(origin: .zero, size: CGSize(width: 11, height: 11))
            
            let stack = UIStackView()
            stack.axis = .horizontal
            stack.alignment = .firstBaseline
            stack.distribution = .fill
            stack.spacing = 10 // 设置子视图间距
            stack.addArrangedSubview(iv)
            stack.addArrangedSubview(label)
            vStackView.addArrangedSubview(stack)
        }
    }
    
    @objc func deleteButtonAction(_ sender: UIButton) {
        deleteActionClosure?(policyView.isSelected)
    }
    
    @objc func cancelButtonAction(_ sender: UIButton) {
        cancelActionClosure?()
    }
}


